var prefix = jsctx + "/tenantPaymentActivity";

var tenantPaymentActivity = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageTenantPaymentActivity",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            remark: $("#remark").val().trim(),
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {
                            field: 'channelType', title: '系统', width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span >微信</span>'
                                } else if (value == '1') {
                                    return '<span >APP</span>'
                                }
                            }
                        },
                        {
                            field: 'payType', title: '支付方式', width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == 'WXPAY') {
                                    return '<span >微信</span>'
                                } else if (value == 'ALIPAY') {
                                    return '<span >支付宝</span>'
                                } else if (value == 'UNIONPAY') {
                                    return '<span >银联</span>'
                                } else if (value == 'ABCPAY') {
                                    return '<span >农行</span>'
                                }
                            }
                        },
                        {field: 'startTime', title: '开始时间', align: 'left', width: '100'},
                        {field: 'endTime', title: '结束时间', align: 'left', width: '100'},
                        {field: 'remark', title: '标签描述', align: 'left', width: '100'},
                        {
                            field: 'status', title: '状态', width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span >正常</span>'
                                } else if (value == '1') {
                                    return '<span >禁止</span>'
                                }
                            }
                        },
                        {field: 'createdTime', title: '创建时间', align: 'left', width: '100'},
                        {
                            title: '操作',
                            field: 'roleId',
                            width: '200', align: 'center',
                            formatter: function (value, row, index) {
                                var a ='';
                                var b='';
                                var c='';
                                if (Tenant_rechargeSetting_delete){
                                    a = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="删除" onclick="tenantPaymentActivity.deleteTenantPaymentActivity(\''
                                        + row.paymentActivityId
                                        + '\')">删除</a> ';
                                }
                                if (Tenant_rechargeSetting_update){
                                    b = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="编辑" onclick="tenantPaymentActivity.editTenantPaymentActivity(\''
                                        + row.paymentActivityId
                                        + '\')">编辑</a> ';
                                    if (row.status == '1') {
                                        c = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="启动" onclick="tenantPaymentActivity.updateStatusForNormal(\''
                                            + row.paymentActivityId
                                            + '\')">启动</a> ';
                                    } else {
                                        c = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="禁用" onclick="tenantPaymentActivity.updateStatusForDisable(\''
                                            + row.paymentActivityId
                                            + '\')">禁用</a> ';
                                    }
                                }
                                return a + b + c;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function addTenantPaymentActivity() {
        location.href = prefix + "/addTenantPaymentActivity";
    }

    function editTenantPaymentActivity(paymentActivityId) {
        location.href = prefix + "/editTenantPaymentActivity/" + paymentActivityId;
    }

    function deleteTenantPaymentActivity(paymentActivityId) {
        layer.confirm('确定要删除选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteTenantPaymentActivity",
                type: "post",
                data: {
                    'paymentActivityId': paymentActivityId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    function goBack() {
        window.history.back();
    }


    function updateStatusForNormal(paymentActivityId) {
        layer.confirm('确定要变成状态为正常？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/updateTenantPaymentActivityStatus",
                type: "post",
                data: {
                    'paymentActivityId': paymentActivityId,
                    'status': '0'
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("更改成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function updateStatusForDisable(paymentActivityId) {
        layer.confirm('确定要变成状态为禁用？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/updateTenantPaymentActivityStatus",
                type: "post",
                data: {
                    'paymentActivityId': paymentActivityId,
                    'status': '1'
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("更改成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        addTenantPaymentActivity: addTenantPaymentActivity,
        deleteTenantPaymentActivity: deleteTenantPaymentActivity,
        editTenantPaymentActivity: editTenantPaymentActivity,
        updateStatusForNormal: updateStatusForNormal,
        updateStatusForDisable: updateStatusForDisable
    };
})();

var addTenantPaymentActivity = (function () {
    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    var isajaxsend = true;

    function save() {
        if (isajaxsend) {
            var formData = new FormData($("#frmTenantPaymentActivity")[0]);
            $("#roleIds").val(getCheckedRoles());
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdateTenantPaymentActivity",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                    isajaxsend = true
                },
                success: function (data) {
                    if (data.status == 200) {
                        isajaxsend = true
                        parent.layer.msg("操作成功");
                        window.history.back();
                    } else {
                        isajaxsend = true
                        parent.layer.alert(data.message)
                    }

                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmTenantPaymentActivity").validate({
            rules: {
                startTime: {
                    required: true
                },
                endTime: {
                    required: true
                },
                remark: {
                    required: true,
                    maxlength: 40
                }
            },
            messages: {
                startTime: {
                    required: icon + "请输入选择开始时间"
                },
                endTime: {
                    required: icon + "请输入选择结束时间"
                },
                remark: {
                    required: icon + "请输入标签描述",
                    maxlength: icon + "标签长度不多于40"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();
